package com.homework;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;

@WebServlet("/fig")
public class ServletConfigDemo extends HttpServlet {
    private ServletConfig servletConfig;

    @Override
    public void init(ServletConfig config) throws ServletException {
        this.servletConfig=config;
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println(servletConfig);



        Enumeration<String> keys = servletConfig.getInitParameterNames();
        while(keys.hasMoreElements()){
            String s = keys.nextElement();
            String valuename = servletConfig.getInitParameter(s);
            System.out.println(s+","+valuename);
        }

        String servletName = servletConfig.getServletName();
        System.out.println(servletName);


        ServletContext servletContext = servletConfig.getServletContext();
        System.out.println(servletContext);
        //获取共享数据
        String value = servletContext.getInitParameter("username");
        System.out.println(value);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
